package com.zh.note.leetcode.graph;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhanghuan
 * @date: 2023/8/12 14:52
 * @description: create it
 */
public class LC_841_canVisitAllRooms {

    public static boolean canVisitAllRooms(List<List<Integer>> rooms) {
        boolean[] visited = new boolean[rooms.size()];
        dfs(0, rooms, visited);
        //检查是否都访问到了
        for (boolean flag : visited) {
            if (!flag) {
                return false;
            }
        }
        return true;

    }

    private static void dfs(int key, List<List<Integer>> rooms, boolean[] visited) {
        if (visited[key]) { // 访问过了
            return;
        }
        visited[key] = true;
        List<Integer> room = rooms.get(key);
        for (int k : room) {
            // 深度优先搜索遍历
            dfs(k, rooms, visited);
        }
    }

    public static void main(String[] args) {
        List<List<Integer>> rooms = new ArrayList<>();
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        List<Integer> list2 = new ArrayList<>();
        list2.add(2);
        List<Integer> list3 = new ArrayList<>();
        list3.add(3);
        List<Integer> list4 = new ArrayList<>();
        rooms.add(list1);
        rooms.add(list2);
        rooms.add(list3);
        rooms.add(list4);
        System.out.println(canVisitAllRooms(rooms));
    }

}
